package com.hncboy.chatgpt.front.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.hncboy.chatgpt.base.domain.mongodb.TextParsing;
import com.hncboy.chatgpt.front.mongodb.TextParsingMapper;
import com.hncboy.chatgpt.front.service.TextParsingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/****************************************
 * @name com.hncboy.chatgpt.front.service.impl
 * @description
 * @author limi
 * @since 2023-06-05 21:02
 ***************************************/
@Slf4j
@Service
public class TextParsingServiceImpl implements TextParsingService {
    @Resource
    TextParsingMapper textParsingMapper;

    @Override
    public String queryInstruct(String txt) {
        List<TextParsing> textParsings = textParsingMapper.selectByKeyword(txt);
        log.info("查询出来的数据为{}", JSONUtil.toJsonStr(textParsings));
        if (CollUtil.isNotEmpty(textParsings)) {
            return textParsings.get(0).getContext();
        } else {
            return null;
        }
    }

    String textCutting(String context, int length) {
        if (context.length() < 1000 / length) {
            return context;
        } else {
            int i = context.lastIndexOf("。");
            String substring = context.substring(0, i);
            return textCutting(substring, length);
        }
    }
}
